using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.VirtualTreeGrid;
using Microsoft.VisualStudio.EnterpriseTools.OperationDesigner;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace EastBancTech.InstallerDefinition.DslEditors.Controls
{
    public class FileSourceBranch: OperationDesignerBranch
    {
        protected Component component;

        public FileSourceBranch(Component component, ColumnDescriptor[] columns)
            : base(columns)
        {
            this.component = component;
        }

        public override int ElementCount
        {
            get { return 1; }
        }

        public override Microsoft.VisualStudio.Modeling.ModelElement GetElement(int index)
        {
            return component.FileSource;
        }

        public override int GetIndexForElement(Microsoft.VisualStudio.Modeling.ModelElement element)
        {
            return 0;
        }

        protected override bool IsExpandable(int index)
        {
            return true;
        }

        protected override IBranch GetExpandedBranch(int index)
        {
            if (index >= this.ElementCount)
            {
                return null;
            }
            FileActionBranch branch = new FileActionBranch(component.FileSource, base.GetColumns());
            if (base.ReadOnly)
            {
                branch.ReadOnly = true;
            }
            return branch;
        }

        protected override string GetCreatorNodeText(int index)
        {
            return "<new File Action>";
        }

        public override int CreatorNodeCount
        {
            get
            {
                return 1;
            }
        }

        protected override LabelEditResult OnCreatorNodeEditCommitted(int index, object value, int insertIndex)
        {
            return base.OnCreatorNodeEditCommitted(index, value, insertIndex);
        }
    }

    public class FileActionBranch : OperationDesignerBranch
    {
        protected ComponentFileSource fileSource;

        public FileActionBranch(ComponentFileSource fileSource, ColumnDescriptor[] columns)
            : base(columns)
        {
            this.fileSource = fileSource;
        }

        public override int ElementCount
        {
            get { return fileSource.FileActions.Count; }
        }

        public override Microsoft.VisualStudio.Modeling.ModelElement GetElement(int index)
        {
            return fileSource.FileActions[index];
        }

        public override int GetIndexForElement(Microsoft.VisualStudio.Modeling.ModelElement element)
        {
            return fileSource.FileActions.IndexOf(element as FileAction);
        }

        protected override bool IsExpandable(int index)
        {
            return true;
        }

        protected override IBranch GetExpandedBranch(int index)
        {
            if (index >= this.ElementCount)
            {
                return null;
            }
            XPathBranch branch = new XPathBranch(fileSource.FileActions[index] as XPathReplacement, base.GetColumns());
            if (base.ReadOnly)
            {
                branch.ReadOnly = true;
            }
            return branch;
        }

        protected override string GetCreatorNodeText(int index)
        {
            return "<new XPath Value>";
        }

        public override int CreatorNodeCount
        {
            get
            {
                return 1;
            }
        }

        protected override LabelEditResult OnCreatorNodeEditCommitted(int index, object value, int insertIndex)
        {
            return base.OnCreatorNodeEditCommitted(index, value, insertIndex);
        }
    }

    public class XPathBranch : OperationDesignerBranch
    {
        protected XPathReplacement xPathReplacement;

        public XPathBranch(XPathReplacement xPathReplacement, ColumnDescriptor[] columns)
            : base(columns)
        {
            this.xPathReplacement = xPathReplacement;
        }

        public override int ElementCount
        {
            get { return xPathReplacement.XPaths.Count; }
        }

        public override Microsoft.VisualStudio.Modeling.ModelElement GetElement(int index)
        {
            return xPathReplacement.XPaths[index];
        }

        public override int GetIndexForElement(Microsoft.VisualStudio.Modeling.ModelElement element)
        {
            return xPathReplacement.XPaths.IndexOf(element as XPathValue);
        }
    }

    public class ComponentHeaderBranch : HeaderBranch, IOperationDesignerInitializeBranch
    {
        protected Component component;
        private ColumnDescriptor[] columns;

        public bool Initialize(object selection, ColumnDescriptor[] columns)
        {
            ShapeElement element = selection as ShapeElement;

            if (element != null)
                component = element.ModelElement as Component;
            else
                component = selection as Component;

            if (this.component != null)
            {
                this.columns = columns;
                this.PopulateHeaders(true);
                return true;
            }
            return false;
        }

        private void PopulateHeaders(bool p)
        {
            List<ChildBranchInfo> list = new List<ChildBranchInfo>(1);

            IBranch branch2 = null;
            branch2 = new FileSourceBranch(this.component, this.columns);
            list.Add(new ChildBranchInfo(branch2, "File Source", "FileSource"));

            base.SetHeaderInfo(list.ToArray(), this.columns);

        }

    }
}
